//
//  MainTabBarController.swift
//  SwiftProject
//
//  Created by wwj on 16/2/23.
//  Copyright © 2016年 wwj. All rights reserved.
//

import UIKit

class TabBarController: UITabBarController, TabBarViewDelegate {
    var tabBarView: TabBarView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initTabBar()
        initAllViewController()
       
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        for view:UIView in self.tabBar.subviews{
            if (view.isKind(of: UIControl.self)){
                view.removeFromSuperview()
            }
        }
    }
    
    //MARK: 自定义TabBar
    fileprivate func initTabBar(){
        tabBarView = TabBarView()
        tabBarView.delegate = self
        tabBarView.frame = self.tabBar.bounds
        self.tabBar.addSubview(tabBarView)
    }
    
    func onClickTabBarView(_ tabBarView: TabBarView, index: NSInteger) {
        self.selectedIndex = index
    }
    
    fileprivate func initAllViewController(){
        initChickViewController(FWHomeViewController(), name: "首页", imageName: "btn_home_normal", selectImageName: "btn_home_selected")
        initChickViewController(FWRankViewController(), name: "直播", imageName: "btn_column_normal", selectImageName: "btn_column_selected")
        initChickViewController(FWDiscoverViewController(), name: "关注", imageName: "btn_live_normal", selectImageName: "btn_live_selected")
        initChickViewController(FWMeViewController(), name: "我的", imageName: "btn_user_normal", selectImageName: "btn_user_selected")
    }

    fileprivate func initChickViewController(_ childVC:UIViewController, name:String, imageName:String, selectImageName:String){
        let naviVC = NavigationController(rootViewController: childVC)
        addChildViewController(naviVC)
        tabBarView.addTabBarButtonWith(name, image: imageName, selectImage: selectImageName)
    }


}
